<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- element-loading-background="rgba(255, 255, 255, 0.8)" -->
      <el-card
        v-loading="loading"
        element-loading-text="拼命加载中..."
        class="tree-card"
      >
        <!-- 树形组件 -->
        <TreeTools
          root
          :node-data="company"
          @add="addFn"
        />
        <el-tree :data="list" :props="defaultProps" default-expand-all>
          <template v-slot="{data}">
            <TreeTools
              :node-data="data"
              @add="addFn"
              @getDepartments="getDepartments"
              @edit="editFn"
            />
          </template>
        </el-tree>
      </el-card>

      <AddDept
        ref="add"
        :show-dialog.sync="showDialog"
        :node-data="nodeData"
        @getDepartments="getDepartments"
      />
    </div>
  </div>
</template>

<script>
import TreeTools from './components/TreeTools'
import AddDept from './components/AddDept'
import { getDepartmentList } from '@/api/departments'
import { listToTree } from '@/utils'
export default {
  name: 'Departments',
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      list: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      loading: true,
      // 控制添加部门的显示和隐藏
      showDialog: false,
      // 当前的部门
      nodeData: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const data = await getDepartmentList()
      console.log(data)
      this.company.name = data.companyName
      this.list = listToTree(data.depts)
      this.loading = false
    },
    addFn(nodeData) {
      this.showDialog = true
      console.log(nodeData)
      this.nodeData = nodeData
    },
    closeFn() {
      this.showDialog = false
    },
    editFn(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      // this.$refs.add.form = nodeData
      // this.nodeData = nodeData
      // 数据回显
      // this.$refs.add.form = { ...this.nodeData }
      // this.$refs.add.getDepts()
      // 发送请求-回显数据
      this.$refs.add.getInfo(nodeData.id)
    }
  }
}
</script>
<style scoped lang="scss">
.tree-card {
  padding: 30px 140px;
  font-size:14px;
}
::v-deep {
  // 修改叶子节点
  // .el-tree-node__expand-icon
  // .el-tree-node__expand-icon.is-leaf::before {

  // }
  // .el-tree-node__expand-icon
  // .el-tree-node__expand-icon.expanded::before {
  //   content: "\e722";
  // }
  // 树状菜单的所有图标
  .el-tree-node__expand-icon {
    font-size: 18px;
    color: #000;
    &::before {
      content: "\e723";
    }
    // 展开的图标
    &.expanded {
      transform: rotate(0deg);
      &::before {
        content: "\e722";
      }
    }
    &.is-leaf {
      color: #000;
      &::before {
        content: "\e7ab";
      }
    }
  }
}
</style>
